﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    public class ReqSyncViewStateHandler : BaseHandler<ReqSyncViewStateData, RespSyncViewStateData>
    {
        public ReqSyncViewStateHandler()
            : base(RESPONSE_SCENARIO.CONNENT, RESPONSE_COMMAND.CONNECT.SYNCVIEWSTATE)
        {
        }

        protected override RespSyncViewStateData OnHandle(Data.MainServiceData.Request request, ReqSyncViewStateData requestData)
        {
            CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);

            if (domain != null)
            {
                CacheData.Client client = domain.GetClient(request.Header.UserIdentity);
                if (client != null)
                {
                    if(!CZEnvironment.Instance.ServerIP.Equals(requestData.ServerIp) || CZEnvironment.Instance.ServerPort != requestData.ServerPort)
                    {
                        Common.Logger.WarningToTag("Handler", "client:{0}请求申请同步视图状态，但是IP或Port不同，不进行同步", request.Header.UserIdentity);
                        return null;
                    }

                    client.IsSyncViewState = false;

                    if (true || requestData.IsImperative)
                    {
                        if (!string.IsNullOrEmpty(domain.SolutionName) && !string.IsNullOrEmpty(domain.ViewStateToken))
                        {
                            // 设置为进入域的状态
                            client.State = CacheData.ClientState.InDomain;
                            RespSyncViewStateData responseData = new RespSyncViewStateData();
                            responseData.ViewStateAge = domain.ViewStateAge;
                            responseData.ViewStateToken = domain.ViewStateToken;
                            return responseData;
                        }
                        else
                        {
                            // 设置为进入域的状态
                            client.State = CacheData.ClientState.InDomain;
                            Common.Logger.WarningToTag("Handler", "client:{0}请求申请同步视图状态，但是服务器的状态无效", request.Header.UserIdentity);
                        }
                    }
                    else
                    {
                        Common.Logger.WarningToTag("Handler", "client:{0}请求申请同步视图状态，但是状态不正确", request.Header.UserIdentity);
                    }
                }
                else
                {
                    Common.Logger.ErrorToTag("Handler", "client:{0},域:{1}请求申请同步视图状态出错，client不存在",
                        request.Header.UserIdentity,
                        request.Header.DomainId);
                }
            }
            else
            {
                Common.Logger.ErrorToTag("Handler", "client:{0}请求申请同步视图状态出错，域:{1}不存在",
                    request.Header.UserIdentity,
                    request.Header.DomainId);
            }
            return null;
        }
    }
}
